package com.whz.workmanageol.utils;

import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;

/**
 * 下载文件需要配置的Content-type Content-Disposition
 */
public class DownloadSet {
    private static final String XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    private static final String ZIP = "application/zip";
    private static final String OTHER = "application/octet-stream";
    public static void setContent(HttpServletResponse response, String fileName){
        fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        String type = null;
        int indexOf = fileName.lastIndexOf(".");
        String fileType = fileName.substring(indexOf + 1);
        switch (fileType) {
            case "zip": type = DownloadSet.ZIP; break;
            case "xlsx": type = DownloadSet.XLSX; break;
            default: type = DownloadSet.OTHER;
        }
        response.setContentType(type);
        response.setHeader("Content-Disposition:", "attachment;filename:" + fileName);
    }
}
